/**
  ******************************************************************************
  * File Name          : file.c
  * Description        : This file provides code for the configuration
  *                      of the file instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ulfileate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "file.h"
#include "lvgl.h"
/* USER CODE BEGIN 0 */

FATFS     fs;
FATFS     *pfs;

FIL fil;
FIL *pf = &fil;

FRESULT   fr ;

uint32_t   write_count=0;
uint32_t   read_count=0;
char       line[20]; /* Line buffer */
char       write_data[]="hello fatfs!\r\n";

uint8_t   SD_State=0;
uint32_t  sd_size = 0;

extern float Temp_Result[12];

//
//void FATFS_TEST (void)
//{
//
//  uint8_t count=0;
//  //MKFS_PARM MKFS;
//  //BYTE work[FF_MAX_SS];
//
//  pfs = &fs;
//
//  for(count=0;count<100;count++)
//    line[count] = count;
//
//  fr = f_mount(pfs, "0:", 1);
//  //if( fr ) return fr;
//
//  if( fr == FR_NO_FILESYSTEM )
//  {
//    /*
//    MKFS.fmt = FM_FAT;
//    MKFS.align = 0;
//    MKFS.au_size = 0;
//    MKFS.n_fat = 0;
//    MKFS.n_root = 0;
//
//    fr = f_mkfs("0:", &MKFS, work, FF_MAX_SS );
//
//    //fr = f_mount(NULL, "1:", 1);
//    //fr = f_mount(pfs, "1:", 1);
//
//    fr = f_open(pf, "0:message.txt", FA_CREATE_NEW|FA_WRITE );
//
//    fr = f_write( pf , write_data , 20 , &write_count );
//    write_count = 0;
//    f_close ( pf );
//
//    */
//	CDC_Transmit_FS((uint8_t*)"Please format SD card",sizeof("Please format SD card"));
//
//  } else if ( fr != FR_OK )
//  {
//    CDC_Transmit_FS((uint8_t*)"SD card Error",sizeof("SD card Error"));
//  }
//
//  fr = f_open(pf, "0:message.txt", FA_READ );
//  if ( fr == FR_NO_FILE )
//  {
//    fr = f_open( pf, "0:message.txt", FA_CREATE_NEW|FA_WRITE|FA_READ );
//
//    fr = f_write( pf , write_data , sizeof(write_data) , &write_count );
//    write_count = 0;
//
//    f_close ( pf );
//    fr = f_open(pf, "0:message.txt", FA_READ );
//
//    CDC_Transmit_FS((uint8_t*)"FR_NO_FILE",sizeof("FR_NO_FILE"));
//
//  }
//
//  fr = f_read( pf , line, sizeof(write_data), &read_count);  /* Read a chunk of data from the source file */
//
//  f_close ( pf );
//
//  fr = f_open(pf, "0:message.txt", FA_WRITE|FA_READ );
//  fr = f_lseek(pf, f_size(pf));
//
//  fr = f_write( pf , write_data , sizeof(write_data) , &write_count );
//
//  f_close ( pf );
//
//  f_unmount("0:");
//
//  CDC_Transmit_FS((uint8_t*)"f_unmount",sizeof("f_unmount"));
//
//}


void FATFS_MOUNT(void)
{
  pfs = &fs;
  fr = f_mount(pfs, "0:", 1);
}


void FATFS_UNMOUNT(void)
{
  f_unmount("0:");
}


void FATFS_Save_Pwr_Val ( uint32_t Timestamp , float Save_Data )
{
	char buf[100];

	Timestamp = Timestamp/1000;

	fr = f_open(pf, "0:Pwr_Val.txt", FA_WRITE|FA_READ );
	if ( fr == FR_NO_FILE )
	{
	fr = f_open( pf, "0:Pwr_Val.txt", FA_CREATE_NEW|FA_WRITE|FA_READ );
	f_close ( pf );
	fr = f_open(pf, "0:Pwr_Val.txt", FA_WRITE|FA_READ );
	}
	fr = f_lseek(pf, f_size(pf));
	lv_snprintf(buf, 100, "%2ldh-%2ldm-%2lds\t%5.2f\r\n\0",\
				Timestamp/3600,Timestamp%3600/60,Timestamp%60, \
				Save_Data );
	fr = f_printf(pf, "%s",buf);
	f_close ( pf );
}



void FATFS_Save_CH_Temp ( uint32_t Timestamp )
{
	char buf[200];

	Timestamp = Timestamp/1000;

	fr = f_open(pf, "0:TEMP.txt", FA_WRITE|FA_READ );
	if ( fr == FR_NO_FILE )
	{
		fr = f_open( pf, "0:TEMP.txt", FA_CREATE_NEW|FA_WRITE|FA_READ );
		fr = f_printf(pf, "Time\tTEMP_CH1\tTEMP_CH2\tTEMP_CH3\tTEMP_CH4\tTEMP_CH5\tTEMP_CH6\tTEMP_CH7\tTEMP_CH8\tTEMP_CH9\tTEMP_CH10\tTEMP_CH11\tTEMP_CH12\r\n");
		f_close ( pf );
		fr = f_open(pf, "0:TEMP.txt", FA_WRITE|FA_READ );
	}
	else if ( !fr )
	{
		fr = f_lseek(pf, f_size(pf));
		lv_snprintf(buf, 200, "%2ldh-%2ldm-%2lds\t%5.2f\t%5.2f\t%5.2f\t%5.2f\t%5.2f\t%5.2f\t%5.2f\t%5.2f\t%5.2f\t%5.2f\t%5.2f\t%5.2f\r\n\0",\
				Timestamp/3600,Timestamp%3600/60,Timestamp%60, \
				Temp_Result[0],Temp_Result[1],Temp_Result[2],Temp_Result[3],\
				Temp_Result[4],Temp_Result[5],Temp_Result[6],Temp_Result[7],\
				Temp_Result[8],Temp_Result[9],Temp_Result[10],Temp_Result[11] \
				);
		fr = f_printf(pf, "%s",buf);
		f_close ( pf );
	}
}


void FATFS_Get_Size ( uint32_t *total , uint32_t *free )
{

	uint32_t fre_clust=0, fre_sect=0, tot_sect=0;
	fr = f_getfree((const TCHAR*)"0", (DWORD*)&fre_clust, &pfs);

    if( fr == FR_OK )
    {
        // 总容量计算方法
        // pfs->csize 该参数代表一个簇占用几个 SD卡物理扇区，每个扇区512字节
        // pfs->n_fatent 簇的数量+2
        // 总容量 = 总簇数*一个簇占用大小
        // 剩余容量 = 剩余簇数*一个簇占用大小
    	tot_sect=(pfs->n_fatent-2)*pfs->csize;
		fre_sect=fre_clust*pfs->csize;
		*total=tot_sect>>1;
		*free=fre_sect>>1;
    }

}




/* USER CODE END 0 */

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
